package com.mx102.kosmos;

import java.util.ArrayList;
import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.input.RemoteInput;

/*
 * + Pozycje na ekranie
 * + Pozycja 
 * 
 * 
 */
public class Kosmos implements ApplicationListener, InputProcessor {

	PerspectiveCamera cam;
	Statek statek;
	Background tlo;
	private ArrayList<Bullet> bullets=new ArrayList<Bullet>();
	
	
	int scrH, scrW;
	
	Music music;
	Sound shootSound;

	@Override
	public void create() {

		statek = new Statek();
		
		tlo=new Background(320, 480, 20);

		//RemoteInput odb=new RemoteInput();
		//Gdx.input=odb;
		
		Gdx.input.setInputProcessor(this);
		
		music=Gdx.audio.newMusic(Gdx.files.internal("space_music.mp3"));
		//music.play();
		
		shootSound=Gdx.audio.newSound(Gdx.files.internal("shot.mp3"));

		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		GL11 gl = Gdx.graphics.getGL11();
		

		gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		//gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
		gl.glEnable(GL11.GL_DEPTH_TEST);
		//gl.glEnable(GL11.GL_LIGHTING);
		//gl.glEnable(GL11.GL_COLOR_MATERIAL);
		gl.glEnable(GL11.GL_TEXTURE_2D);
		gl.glDepthFunc(GL11.GL_LEQUAL);
		gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
		gl.glEnable (GL11.GL_BLEND);
		gl.glBlendFunc (GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		//gl.glBlendFunc(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ZERO);
		cam.update();

		gl.glMatrixMode(GL11.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glLoadMatrixf(cam.projection.val, 0);
		gl.glMatrixMode(GL11.GL_MODELVIEW);
		gl.glLoadMatrixf(cam.view.val, 0);


		if(Gdx.input.getRoll()>10) statek.moveRight();
		else if(Gdx.input.getRoll()<-10) statek.moveLeft();
		else statek.stop();
		
		
		float delta=Gdx.graphics.getDeltaTime();
		statek.render(gl, delta);
		
		for(Iterator<Bullet> it=bullets.iterator(); it.hasNext();) {
			Bullet b=(Bullet) it.next();
			
			if(b.toDestroy()) it.remove();
			else {
				b.render(gl, delta);
				
			}
		
		}
		
		tlo.render(gl,delta,cam);
		



	}

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float) width / (float) height;
		scrW = width;
		scrH = height;

		cam = new PerspectiveCamera(67, 2f * aspectRatio, 2f);
		cam.position.set(0, 250, -120);
		cam.rotate(90, -1, 0, 0);
		cam.near = 10f;
		cam.far = 500f;

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		
		
		bullets.add(new Bullet(statek.getPosition()));
		shootSound.play();
		
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		
		return false;
	}

}
